package createtask;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import page.PageState;
import usr.Usr;
import weibo4j.WeiboException;
import channel.Channel;
import channel.ChannelMail;
import channel.ChannelTime;
import channel.ChannelTimedoutWeibo;
import channel.ChannelWeibo;
import channel.ChannelYmail;

public class createtaskThis extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@SuppressWarnings("deprecation")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	{	//create a THIS channel and save in Session
		try {
			HttpSession sess = req.getSession();
			Usr usr = (Usr)sess.getAttribute("usr");
			if(usr == null)
			{
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			String sid = req.getParameter("thisid");

			if(sid == null)
			{
				req.setAttribute("error_cause", PageState.UNKNOWED_ERROR);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			int id = Integer.parseInt(sid);
			
			if(id == Channel.DATE_TIME)	//Time
			{
				int everyid = Integer.parseInt(req.getParameter("everyid"));
				
				String sMonth = req.getParameter("Month");
				String sDay = req.getParameter("Day");
				String sHour = req.getParameter("Hour");
				String sMin = req.getParameter("Min");
					
				ChannelTime thisTime = new ChannelTime();
				Date din = new Date();
				din.setMinutes(Integer.parseInt(sMin));
				if(everyid >= ChannelTime.EVERY_DAY)
					din.setHours(Integer.parseInt(sHour));					
				if(everyid >= ChannelTime.EVERY_MONTH)
					din.setDate(Integer.parseInt(sDay));
				if(everyid >= ChannelTime.EVERY_YEAR)
					din.setMonth(Integer.parseInt(sMonth));
					
				sess.setAttribute("this", thisTime);
				sess.setAttribute("thisType", id);
				resp.sendRedirect("newtask.jsp");
			}
			
			else if(id == Channel.WEIBO)	//Weibo
			{
				String key = req.getParameter("key");
				String target = req.getParameter("target");
				ChannelWeibo thisWeibo = null;
				try {
					thisWeibo = new ChannelWeibo(target, key);
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				sess.setAttribute("this", thisWeibo);
				sess.setAttribute("thisType", id);
				resp.sendRedirect("newtask.jsp");
			}

			else if(id == Channel.EMAIL)	//email
			{
				String from = req.getParameter("from");
				String key = req.getParameter("key");
				
				ChannelMail thisMail = new ChannelMail();
				thisMail.setFrom(from);
				thisMail.setKey(key);
				
				sess.setAttribute("this", thisMail);
				sess.setAttribute("thisType", id);
				resp.sendRedirect("newtask.jsp");
			}
			
			else if(id == Channel.YMAIL)	//ymail
			{
				String user = req.getParameter("user");
				String mailType = req.getParameter("mailType");
				String username = user + "@" + mailType;
				String pwd = req.getParameter("pwd");
				String from = req.getParameter("from");
				String key = req.getParameter("key");
				
				ChannelYmail thisYmail = new ChannelYmail();
				thisYmail.setYmail(username);
				thisYmail.setPassword(pwd);
				thisYmail.setFrom(from);
				thisYmail.setKey(key);
				
				sess.setAttribute("this", thisYmail);
				sess.setAttribute("thisType", id);
				resp.sendRedirect("newtask.jsp"); 
			}
			
			else if(id == Channel.TIMEDOUTWEIBO)	//TimeoutWeibo
			{
				String key = req.getParameter("key");
				String target = req.getParameter("target");
				String eMonth = req.getParameter("eMonth");
				String eDay = req.getParameter("eDay");
				String eHour = req.getParameter("eHour");
				String eMin = req.getParameter("eMin");
				Date endTime = new Date();
				endTime.setMonth(Integer.parseInt(eMonth));
				endTime.setDate(Integer.parseInt(eDay));
				endTime.setHours(Integer.parseInt(eHour));
				endTime.setMinutes(Integer.parseInt(eMin));
				
				ChannelTimedoutWeibo thisTimedoutWeibo = null;

				try {
					thisTimedoutWeibo = new ChannelTimedoutWeibo();
					thisTimedoutWeibo.setDeadline(endTime);
					thisTimedoutWeibo.setKey(key);
					thisTimedoutWeibo.setTargetScreenName(target);
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				
				sess.setAttribute("this", thisTimedoutWeibo);
				sess.setAttribute("thisType", id);
				resp.sendRedirect("newtask.jsp");
			}

		}
		
		catch (IOException | ServletException  e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
			
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	{	//set attribute in order to allow "this.jsp" show much content
		try {
			HttpSession sess = req.getSession();
			Usr usr = (Usr)sess.getAttribute("usr");
			if(usr == null) {
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			int id = Integer.parseInt(req.getParameter("thisid"));
			
			req.setAttribute("thisid", id);
			
			String severyid = req.getParameter("everyid");
			if(severyid != null)
			{
				int everyid = Integer.parseInt(severyid);
				req.setAttribute("everyid", everyid);
			}
			
			req.getRequestDispatcher("this.jsp").forward(req, resp);
			return;
		}
		
		catch (IOException | ServletException e) {
			try {
				resp.sendRedirect("u.html");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
